const data = require("./data");

const { PROMISE_PENDDING, PROMISE_FULFILLED, PROMISE_REJECTED } = data;
class MyPromise {
  constructor(executor) {
    this.status = PROMISE_PENDDING;
    this.value = undefined;
    this.reason = undefined;
    const resolve = (value) => {
      if (this.status === PROMISE_PENDDING) {
        this.status = PROMISE_FULFILLED;
        this.value = value;
      }
    };

    const rejected = (err) => {
      if (this.status === PROMISE_PENDDING) {
        this.status = PROMISE_REJECTED;
        this.reason = err;
      }
    };

    executor(resolve, rejected);
  }

  then = (onFulfilled, onRejected) => {
    if (onFulfilled && this.status === PROMISE_FULFILLED) {
      onFulfilled(this.value);
    }
    if (onRejected && this.status === PROMISE_REJECTED) {
      onRejected(this.reason);
    }
  };
}
